//
//  HomeViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/21.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "HomeViewController.h"
#import "AppDelegate.h"
#import "HomePageModel.h"
#import "OrangeNavController.h"
#import "LoginViewController.h"
#import "WSScrollLabel.h"
#import "JXCircleSlider.h"
#import "SystemMessageeViewController.h"
#import "NoticeDetailViewController.h"
#import "WindowView.h"
#import "FinishInfoViewController.h"
#import "BannerModel.h"
#import "SDCycleScrollView.h"
#import "HomeAdView.h"
#import "HomeAdModel.h"
#import "BorrowApplyViewController.h"
#import "HomeBannerViewController.h"
#import "HomeAdDetailViewController.h"
#import "BorrowApplyViewController.h"
#import "BorrowRecodeController.h"
#import <CoreLocation/CoreLocation.h>
#import "ImmediatelyRepayController.h"


@interface HomeViewController ()<SDCycleScrollViewDelegate,CLLocationManagerDelegate>

@property (nonatomic, strong)JXCircleSlider * JXCircleSliderView;
@property (nonatomic,strong) HomeAdView* adView;
@property (nonatomic, weak)SDCycleScrollView * topImageView;
@property (nonatomic, weak)UIView * messageView;
@property (nonatomic, weak)UIImageView * trumpetImage;
@property (nonatomic, strong)WSScrollLabel * scrollLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic,strong)NSArray * adModels;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, weak)UILabel * topMoneyLabel;
@property (nonatomic, weak)UILabel * explationLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * explationLeftLabel;
@property (nonatomic, weak)UILabel * explationRightLabel;
@property (nonatomic, weak)UILabel * explationLeftValueLabel;
@property (nonatomic, weak)UILabel * explationRightValueLabel;
@property (nonatomic, weak)UILabel * minMoneyLabel;
@property (nonatomic, weak)UILabel * maxMoneyLabel;
@property (nonatomic, weak)UIButton * LoginLabel;
@property (nonatomic, weak)UILabel * bottomLabel;
@property (nonatomic,strong)UIButton * messageCountBtn;
@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)WindowView * windowView;
@property (nonatomic,strong)NSArray * banners;
@property (nonatomic,strong)HomePageModel * pageModel;
@property (nonatomic, copy)NSString * nameString;
@property (nonatomic, copy)NSString * ID;

//@property (nonatomic, weak)UIScrollView * scrollView;
//@property (nonatomic, strong)UIView * BView;
@property (nonatomic, copy)NSString * linkUrl;

@property (nonatomic, copy)NSString * state;


@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, copy)NSString * applyNo;

@end

@implementation HomeViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    NSString * str = DEF_GET_OBJECT(userToken);
//    
//    NSLog(@"==%@",str);
    
    [self loadScrollLabel];
    [self loadState];
    [self getHomeBanners];
    [self getUnReadMessages];
//    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xf6f6f6);
    self.navigationItem.title = @"";
    
    [self setupNoti];
    [self setupNavBar];
    [self setupUI];
//    [self startLocation];
}

//- (void)loadData{
//    
//    NSDictionary * parameters = @{@"page":@1,@"limit":@10};
//    
//    [[NetworkTool manager] POST:@"loan/loadBorrowingRecordList.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
//        
////                NSLog(@"responseObject===%@",responseObject);
//        
//        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
//        
//        if ([code isEqualToString:@"0"]) {
//            
//            NSArray * array = responseObject[@"data"];
//            NSDictionary * dict = array.firstObject;
////            NSLog(@"dict===%@",dict);
//            self.applyNo = [NSString stringWithFormat:@"%@",dict[@"applyNo"]];
////            NSLog(@"dict===%@",self.applyNo);
//        }
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"==%@",error);
//        
//    }];
//}

- (void)loadState{
    
    [[NetworkTool manager] POST:@"common/getDefaultCreditLimit.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
//        NSLog(@"===%@",responseObject);
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            self.pageModel = [[HomePageModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
            [self updatePage];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"==%@",error);
        
    }];
    
}

-(void)updatePage
{

    switch ([self.pageModel.currentUserStatus integerValue]) {
        case 0:
        {
            self.JXCircleSliderView.enabled = YES;
            self.JXCircleSliderView.angle = 80;
            self.topMoneyLabel.text =[NSString stringWithFormat:@"%d元",[self.pageModel.minLoanCredit intValue]];
            [self.LoginLabel setTitle:@"登录查看借款额度" forState:UIControlStateNormal];
            self.explationRightValueLabel.text = [NSString stringWithFormat:@"%.2f%%-%.2f%%",[self.pageModel.minLoanDayRate doubleValue]*100,[self.pageModel.maxLoanDayRate doubleValue]*100];

        }
            break;
        case 1:
        {
            [self.LoginLabel setTitle:@"立即申请" forState:UIControlStateNormal];
            self.JXCircleSliderView.angle = [self angelWithMoney:[self.pageModel.myLoanCredit intValue]];;
            self.topMoneyLabel.text =[NSString stringWithFormat:@"%d元",[self.pageModel.myLoanCredit intValue]];
            self.JXCircleSliderView.enabled = NO;
            self.explationRightValueLabel.text = [NSString stringWithFormat:@"%.2f%%",[self.pageModel.myLoanDayRate doubleValue]*100];

        }
            break;
        case 2:
        {        [self.LoginLabel setTitle:@"立即申请" forState:UIControlStateNormal];
            self.JXCircleSliderView.enabled = NO;
            self.JXCircleSliderView.angle = [self angelWithMoney:[self.pageModel.myLoanCredit intValue]];;
            self.topMoneyLabel.text =[NSString stringWithFormat:@"%d元",[self.pageModel.myLoanCredit intValue]];
            self.explationRightValueLabel.text = [NSString stringWithFormat:@"%.2f%%",[self.pageModel.myLoanDayRate doubleValue]*100];

        }
            break;
        default:
        {
            
            self.JXCircleSliderView.angle = [self angelWithMoney:[self.pageModel.myLoanCredit intValue]];;
            self.topMoneyLabel.text =[NSString stringWithFormat:@"%d元",[self.pageModel.myLoanCredit intValue]];
            [self.LoginLabel setTitle:@"立即还款" forState:UIControlStateNormal];
            self.JXCircleSliderView.enabled = NO;
            self.explationRightValueLabel.text = [NSString stringWithFormat:@"%.2f%%",[self.pageModel.myLoanDayRate doubleValue]*100];

        }
            break;
    }
    self.explationLeftValueLabel.text = [NSString stringWithFormat:@"%d天",[self.pageModel.loanDays intValue]];
    
    self.minMoneyLabel.text = [NSString stringWithFormat:@"%ld元",[self.pageModel.minLoanCredit integerValue]];
    self.maxMoneyLabel.text = [NSString stringWithFormat:@"%ld元",[self.pageModel.maxLoanCredit integerValue]];

}

-(int)angelWithMoney:(NSInteger)money
{
    NSInteger addAngel =0;
    if(money>=[self.pageModel.minLoanCredit intValue])
    {
        
        NSInteger moreMoney = money - [self.pageModel.minLoanCredit intValue];
        
        
        NSInteger maxMoreMoney = [self.pageModel.maxLoanCredit intValue]-[self.pageModel.minLoanCredit intValue];
        
        addAngel = moreMoney*1.0/maxMoreMoney*200;

    }
    return 80+addAngel;
}
-(int)moneyWidthAngel:(int)angel
{
    return  (angel-80)*1.0/200*([self.pageModel.maxLoanCredit intValue]-[self.pageModel.minLoanCredit intValue]) + [self.pageModel.minLoanCredit intValue];
}



- (void)loadScrollLabel{
    
    NSDictionary * parameters = @{@"page":@"1",@"limit":@"1"};
    
    [[NetworkTool shareManager] POST:@"content/announce/loadAnnounceList.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
//        NSLog(@"===%@",responseObject);
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            self.adModels = [HomeAdModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];
            self.adView.models = self.adModels;
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"==%@",error);
        
    }];
    
}


-(void)getHomeBanners
{
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile loadBannerList] Param:[para toDictionary] andWaitStr:nil waitView:nil success:^(id  _Nullable responseObject) {
        
        if([[responseObject objectForKey:@"code"] intValue]==0)
        {
            
            self.banners = [BannerModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];
            NSMutableArray*urls = [NSMutableArray array];
            [self.banners enumerateObjectsUsingBlock:^(BannerModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if(obj.uri.length)
                {
                    [urls addObject:obj.uri];
                }
            }];
            self.topImageView.imageURLStringsGroup = urls;
            
            
            
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}







- (void)setupNavBar{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    
    
    
    
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 50 , 30);
    [backButton setImage:[UIImage imageNamed:@"铃铛"] forState:UIControlStateNormal];
    [backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [backButton addTarget:self action:@selector(pushToSystemMessage) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:backButton];
    
    self.messageCountBtn = [[UIButton alloc] initWithFrame:CGRectMake(8, 5, 12, 12)];
    self.messageCountBtn.layer.cornerRadius = 6;
    self.messageCountBtn.userInteractionEnabled = NO;
    self.messageCountBtn.layer.masksToBounds = YES;
    [self.messageCountBtn.titleLabel setFont:[UIFont systemFontOfSize:9]];
    self.messageCountBtn.backgroundColor = TOTAL_COLOR;
    self.messageCountBtn.contentEdgeInsets = UIEdgeInsetsMake(2, 3, 2, 3);
    [view addSubview:self.messageCountBtn];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:view];

    
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    self.navigationController.navigationBar.translucent=YES;
    UIColor *color=[UIColor clearColor];
    CGRect rect =CGRectMake(0,0,self.view.frame.size.width,64);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:(UIBarMetricsDefault)];
    
    self.navigationController.navigationBar.clipsToBounds=YES;
}


- (void)setupNoti{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didCancelButton) name:@"didHomeCancelButton" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGoFinishVC) name:@"didHomeGoFinishVC" object:nil];
}

- (void)setupUI{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//    
//    scrollView.backgroundColor = BACK_COLOR;
////    水平方向
//    scrollView.showsHorizontalScrollIndicator = NO;
////    垂直方向
//    scrollView.showsVerticalScrollIndicator = NO;
////    不管有没有设置contentSize,总是有弹簧效果(下拉刷新)
//    scrollView.alwaysBounceHorizontal = NO;
//    scrollView.alwaysBounceVertical = YES;
////    是否有弹簧效果
//    scrollView.bounces = YES;
////    scrollView.contentSize = self.view.bounds.size;
//    
//    self.scrollView = scrollView;
//    
//    [self.view addSubview:self.scrollView];
//    
//    
//    self.BView = [[UIView alloc] initWithFrame:self.view.bounds];
//    
//    self.BView.backgroundColor = [UIColor whiteColor];
//    self.BView.userInteractionEnabled = YES;
//    
//    [self.scrollView addSubview:self.BView];
    
    
    SDCycleScrollView * imageView = [[SDCycleScrollView alloc] init];
    
    imageView.backgroundColor = TOTAL_COLOR;
    
    self.topImageView = imageView;
    self.topImageView.delegate = self;
    [self.view addSubview:self.topImageView];
    
    
    
    UIView * messageView = [UIView new];
    
    messageView.backgroundColor = [UIColor whiteColor];
    messageView.userInteractionEnabled = YES;
    
    self.messageView = messageView;
    
    [self.view addSubview:self.messageView];
    

    
    
    
    
    
    
    UIImageView * trumpetImage = [UIImageView new];
    
    trumpetImage.image = [UIImage imageNamed:@"首页公告"];
    
    self.trumpetImage = trumpetImage;
    
    [self.messageView addSubview:self.trumpetImage];
    
    
    
    
    self.adView = [[HomeAdView alloc] init];
    
    [self.adView setClickBlock:^(HomeAdModel *model) {
        
        HomeAdDetailViewController* vc = [HomeAdDetailViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.messageView addSubview:self.adView];

    
    
    
//    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(40,5.5, 250, 30)];
////    label3.backgroundColor = [UIColor redColor];
//    label3.textColor = ColorWithRGB(0x7f7f7f);
//    label3.font = [UIFont systemFontOfSize:12 * SCREENH6];
//    label3.text =@"噜啦啦噜啦啦噜啦噜啦噜，噜啦噜啦噜啦噜啦噜啦噜~~~";
//    label3.clipsToBounds = YES;
//    
//    
//    [self.messageView addSubview:label3];
//    
//    CGRect frame = label3.frame;
//    frame.origin.x = 200;
//    label3.frame = frame;
//    [UIView beginAnimations:@"testAnimation"context:NULL];
//    [UIView setAnimationDuration:8.8f];
//    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//    [UIView setAnimationDelegate:self];
//    [UIView setAnimationRepeatAutoreverses:NO];
//    [UIView setAnimationRepeatCount:999999];
//    frame = label3.frame;
//    frame.origin.x = 40;
//    label3.frame = frame;
//    [UIView commitAnimations];
    
    
    
    
      
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = [UIColor whiteColor];
    
    self.bottomView = bottomView;
    
    [self.view addSubview:self.bottomView];
    
    
    UILabel * topMoneyLabel = [UILabel new];
    
    topMoneyLabel.textColor = TOTAL_COLOR;
    topMoneyLabel.font = [UIFont fontWithName:@"CTZhongYuanSJ" size:16 * SCREENH6];
    topMoneyLabel.textAlignment = NSTextAlignmentCenter;
    topMoneyLabel.text = @"0元";

    self.topMoneyLabel = topMoneyLabel;
    
    [self.view addSubview:self.topMoneyLabel];
    
    
    JXCircleSlider *slider = [[JXCircleSlider alloc] initWithFrame:CGRectMake(62.5 * SCREENH6, 40 * SCREENH6, 250 * SCREENH6, 250 * SCREENH6)];
    
    [slider addTarget:self action:@selector(newValue:) forControlEvents:UIControlEventValueChanged];
    
    [slider changeAngle:80];
    
    slider.transform = CGAffineTransformRotate(slider.transform, M_PI_2);
    
    self.JXCircleSliderView = slider;
    
    [self.bottomView addSubview:self.JXCircleSliderView];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = TOTAL_COLOR;
    lineView.alpha = 0.1;
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    
    UILabel * explationLabel = [UILabel new];
    
    explationLabel.text = @"借钱金额（元）";
    explationLabel.textColor = ColorWithRGB(0x303c41);
//    explationLabel.font = [UIFont fontWithName:@"MocrosoftYaHei" size:12 * SCREENH6];
    explationLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    explationLabel.textAlignment = NSTextAlignmentCenter;
    
    self.explationLabel = explationLabel;
    
    [self.view addSubview:self.explationLabel];
    
    UILabel * explationLeftLabel = [UILabel new];
    
    explationLeftLabel.text = @"借钱期限";
    explationLeftLabel.textColor = ColorWithRGB(0x303c41);
    //    explationLabel.font = [UIFont fontWithName:@"MocrosoftYaHei" size:12 * SCREENH6];
    explationLeftLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    
    self.explationLeftLabel = explationLeftLabel;
    
    [self.view addSubview:self.explationLeftLabel];
    
    
    UILabel * explationRightLabel = [UILabel new];
    
    explationRightLabel.text = @"日利率";
    explationRightLabel.textColor = ColorWithRGB(0x303c41);
    //    explationLabel.font = [UIFont fontWithName:@"MocrosoftYaHei" size:12 * SCREENH6];
    explationRightLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    
    self.explationRightLabel = explationRightLabel;
    
    [self.view addSubview:self.explationRightLabel];
    
    
    UILabel * explationLeftValueLabel = [UILabel new];
    
//    explationLeftValueLabel.text = @"14天";
    explationLeftValueLabel.textColor = ColorWithRGB(0x303c41);
    //    explationLabel.font = [UIFont fontWithName:@"MocrosoftYaHei" size:12 * SCREENH6];
    explationLeftValueLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    
    self.explationLeftValueLabel = explationLeftValueLabel;
    
    [self.view addSubview:self.explationLeftValueLabel];
    
    
    UILabel * explationRightValueLabel = [UILabel new];
    
    explationRightValueLabel.textColor = ColorWithRGB(0x303c41);
    //    explationLabel.font = [UIFont fontWithName:@"MocrosoftYaHei" size:12 * SCREENH6];
    explationRightValueLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    
    self.explationRightValueLabel = explationRightValueLabel;
    
    [self.view addSubview:self.explationRightValueLabel];
    
    
    UILabel * minMoneyLabel = [UILabel new];
    
//    minMoneyLabel.text = @"500元";
    minMoneyLabel.textColor = TOTAL_COLOR;
    minMoneyLabel.font = [UIFont fontWithName:@"CTZhongYuanSJ" size:15 * SCREENH6];
    minMoneyLabel.textAlignment = NSTextAlignmentCenter;
    
    self.minMoneyLabel = minMoneyLabel;
    
    [self.view addSubview:self.minMoneyLabel];
    
    UILabel * maxMoneyLabel = [UILabel new];
    
//    maxMoneyLabel.text = @"5000元";
    maxMoneyLabel.textColor = TOTAL_COLOR;
    maxMoneyLabel.font = [UIFont fontWithName:@"CTZhongYuanSJ" size:15 * SCREENH6];
    maxMoneyLabel.textAlignment = NSTextAlignmentCenter;
    
    self.maxMoneyLabel = maxMoneyLabel;
    
    [self.view addSubview:self.maxMoneyLabel];
    
    
    UIButton * loginButton = [UIButton new];
    //300 * 42
    [loginButton setTitle:@"登录查看借款额度" forState:UIControlStateNormal];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"去还款"] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(didLoginButton:) forControlEvents:UIControlEventTouchUpInside];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.LoginLabel = loginButton;
    
    [self.view addSubview:self.LoginLabel];
    
    
    UILabel * bottomLabel = [UILabel new];
    
    bottomLabel.text = @"51易借不向在校学生提供借贷服务";
    bottomLabel.textColor = ColorWithRGB(0xcccaca);
//    bottomLabel.font = [UIFont fontWithName:@"CTZhongYuanSJ" size:10.5 * SCREENH6];
    bottomLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.bottomLabel = bottomLabel;
    
    [self.view addSubview:self.bottomLabel];
}


-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    BannerModel * dict = self.banners[index];
    
    if (dict.linkUrl) {
    
        HomeBannerViewController* vc = [HomeBannerViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        vc.banner = self.banners[index];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}



- (void)viewDidLayoutSubviews{
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(215 * SCREENH6);
    }];
    
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(6 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-6 * SCREENH6);
        make.top.mas_equalTo(self.topImageView.mas_bottom).offset(5 * SCREENH6);
        make.height.mas_equalTo(37 * SCREENH6);
    }];
    
    [self.trumpetImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.messageView);
        make.leading.mas_equalTo(self.messageView).offset(7 * SCREENH6);
        make.width.mas_equalTo(18 * SCREENH6);
        make.height.mas_equalTo(16 * SCREENH6);
    }];
    [self.adView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.messageView);
        make.leading.mas_equalTo(self.messageView).offset(25 * SCREENH6);
        make.trailing.mas_equalTo(self.messageView).offset(7 * SCREENH6);
        make.height.mas_equalTo(37 * SCREENH6);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.messageView);
        make.top.mas_equalTo(self.messageView.mas_bottom).offset(5 * SCREENH6);
        make.height.mas_equalTo(348 * SCREENH6);
    }];
    
    [self.topMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view).offset(8 * SCREENH6);
        make.top.mas_equalTo(self.bottomView).offset(23 * SCREENH6);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topMoneyLabel);
        make.top.mas_equalTo(self.JXCircleSliderView.mas_top).offset(52 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.width.mas_equalTo(125 * SCREENH6);
        make.top.mas_equalTo(self.explationLabel.mas_bottom).offset(9 * SCREENH6);
    }];
    
    [self.explationLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(9 * SCREENH6);
    }];
    
    [self.explationRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.explationLeftLabel);
    }];
    
    [self.explationLeftValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.explationLeftLabel);
        make.top.mas_equalTo(self.explationLeftLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.explationRightValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.explationRightLabel);
        make.top.mas_equalTo(self.explationRightLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.minMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.explationLeftValueLabel.mas_bottom).offset(40 * SCREENH6);
        make.leading.mas_equalTo(self.bottomView).offset(60 * SCREENH6);
    }];
    
    [self.maxMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.bottomView).offset(- 45 * SCREENH6);
        make.top.mas_equalTo(self.minMoneyLabel);
    }];
    
    
    [self.LoginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.minMoneyLabel.mas_bottom).offset(40 * SCREENH6);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
    }];
    
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.LoginLabel.mas_bottom).offset(17 * SCREENH6);
    }];
}


//开始定位
- (void)startLocation {
    if ([CLLocationManager locationServicesEnabled]) {
        //        CLog(@"--------开始定位");
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        //控制定位精度,越高耗电量越
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        // 总是授权
        [self.locationManager requestAlwaysAuthorization];
        self.locationManager.distanceFilter = 10.0f;
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([error code] == kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }
    if ([error code] == kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
}

//定位代理经纬度回调
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    CLLocation *newLocation = locations.firstObject;
    CLLocationCoordinate2D coordinate = newLocation.coordinate;

//    NSLog(@"您的当前位置:经度：%f,纬度：%f",coordinate.longitude,coordinate.latitude);
    
    NSString * longitude = [NSString stringWithFormat:@"%f",coordinate.longitude];
    NSString * latitude = [NSString stringWithFormat:@"%f",coordinate.latitude];
    
//    NSDictionary * parameters = @{@"longitude":longitude,@"latitude":latitude};
//    
//    [[NetworkTool shareManager] POST:@"user/uploadUserLocation.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
//        
//        NSLog(@"responseObject==%@",responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"==%@",error);
//        
//    }];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setValue:longitude forKey:@"longitude"];
    [defaults setValue:latitude forKey:@"latitude"];
    
    [defaults synchronize];

    [manager stopUpdatingLocation];
}



- (void)setupWindow{
    
    UIView * backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.55;
    self.backView = backView;
    
    [self.tabBarController.view addSubview:self.backView];
    
    self.windowView = [[WindowView alloc] initWithFrame:CGRectMake(52 * SCREENH6, 250 * SCREENH6, 270 * SCREENH6, 135 * SCREENH6)];
    self.windowView.state = @"home";
//    [self.navigationController.view addSubview:self.windowView];
    [self.tabBarController.view addSubview:self.windowView];
    
    //下面这段代码是 将bottomView调整到父视图的最上面
    [self.view bringSubviewToFront:self.windowView];
}



-(void)newValue:(JXCircleSlider*)slider{
    
    NSLog(@"newValue:%d",slider.angle);
    self.topMoneyLabel.text = [NSString stringWithFormat:@"%d元",[self moneyWidthAngel:slider.angle]];
}

- (void)tapAction{
    
    HIDDEN_TAB;
    
    NoticeDetailViewController * detailVC = [NoticeDetailViewController new];
    
    detailVC.titleString = self.nameString;
    detailVC.ID = self.ID;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    SHOW_TAB;
}


- (void)didLoginButton:(UIButton *)button{
    
    if(self.pageModel){
        switch ([self.pageModel.currentUserStatus intValue]) {
            case 0:
            {
                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                
                LoginViewController * loginVC = [LoginViewController new];
                
                loginVC.typeString = @"1";
                
                OrangeNavController * navVC = [[OrangeNavController alloc] initWithRootViewController:loginVC];
                
                appDelegate.window.rootViewController = navVC;
                
                
            }
                break;
            case 1:
            {
                [self setupWindow];
                
            }
                break;
            case 2:
            {
                HIDDEN_TAB;
                BorrowApplyViewController * vc = [BorrowApplyViewController new];
//                vc.hidesBottomBarWhenPushed = YES;
                vc.from = @"home";
                [self.navigationController pushViewController:vc animated:YES];
                SHOW_TAB;
            }
                break;
            case 3:
            {
//                BorrowRecodeController * immediateVC = [BorrowRecodeController new];
//                immediateVC.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:immediateVC animated:YES];
                
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                
                NSDictionary * parameters = @{@"page":@1,@"limit":@10};
                
                [[NetworkTool manager] POST:@"loan/loadBorrowingRecordList.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
                    
                    NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                    
                    if ([code isEqualToString:@"0"]) {
                        
                        [MBProgressHUD hideHUDForView:self.view animated:YES];
                        
                        NSArray * array = responseObject[@"data"];
                        NSDictionary * dict = array.firstObject;

                        HIDDEN_TAB;
                        ImmediatelyRepayController * immediateVC= [ImmediatelyRepayController new];
                        immediateVC.loanID = [NSString stringWithFormat:@"%@",dict[@"applyNo"]];
//                        immediateVC.borrow = @"home";
                        
                        [self.navigationController pushViewController:immediateVC animated:YES];
                        
                        SHOW_TAB;
                    }else{
                        
                        NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"message"]];
                        [self showHint:string];
                    }
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    NSLog(@"==%@",error);
                    
                }];
                
            }
                break;
            default:
                break;
        }
        
    }
    
    
    

    
}


- (void)didCancelButton{
    
    [self.backView removeFromSuperview];
    [self.windowView removeFromSuperview];
}


- (void)didGoFinishVC{
    
    [self.backView removeFromSuperview];
    [self.windowView removeFromSuperview];
    
    HIDDEN_TAB;
    
    FinishInfoViewController * finishVc = [FinishInfoViewController new];
    
    [self.navigationController pushViewController:finishVc animated:YES];
    
    SHOW_TAB;
}


- (void)pushToSystemMessage{
    
    if([DEF_GET_OBJECT(userToken) length]){
    HIDDEN_TAB;
    
    SystemMessageeViewController * msgVC = [SystemMessageeViewController new];
        msgVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:msgVC animated:YES];
    
    SHOW_TAB;
    }else
    {
        [self showHint:@"请先登录"];
    }
}


-(void)getUnReadMessages
{
    if([DEF_GET_OBJECT(userToken) length])
    {
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile getUnReadMsgCount] Param:[para toDictionary] andWaitStr:@"" waitView:nil success:^(id  _Nullable responseObject) {
        
//        NSLog(@"==%@",responseObject);
        
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            int count = [[responseObject objectForKey:@"data"] intValue];
            
            if (count == 0) {
                self.messageCountBtn.hidden = YES;
            }else{
                
                self.messageCountBtn.hidden = NO;
                [self.messageCountBtn setTitle:[NSString stringWithFormat:@"%d",count] forState:UIControlStateNormal];
                [self.messageCountBtn sizeToFit];
                self.messageCountBtn.height = 12;
            }
//            self.messageCountBtn.hidden = count==0;
//            [self.messageCountBtn setTitle:[NSString stringWithFormat:@"%d",count] forState:UIControlStateNormal];
//            [self.messageCountBtn sizeToFit];
//            self.messageCountBtn.height = 12;
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    }else
    {
        self.messageCountBtn.hidden = YES;
    }
    
}


-  (BOOL) isBlankString:(NSString *)string {
    
    if (string == nil || string == NULL) {
        
        return YES;
        
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        
        return YES;
        
    }
    
    return NO;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
